import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class MD5 {
    public static String md5(String input) {
        try {
            // 创建一个MessageDigest对象，指定MD5算法
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 对输入的字符串进行摘要计算
            byte[] digest = md.digest(input.getBytes());
            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5加密算法不存在", e);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要加密的的字符串：");
        String input = scanner.nextLine();
        String md5 = md5(input);
        System.out.println("MD5加密后的字符串：" + md5);
    }
}

